#include "stdafx.h"
#include "ServiceImp.h"


ServiceImp::ServiceImp()
:m_theEmptyWork(m_theIoSvc),
m_theTimmer(m_theIoSvc,boost::posix_time::seconds(1)),
m_theStrand(m_theIoSvc),
m_theUpdateCycle(1),
m_bContinueRunning(true)
{
}

void
ServiceImp::init(ServiceData& data)
{
	LOG_DEBUG("Failed, pleased don't call me!")
}

void
ServiceImp::start()
{
	m_theThreads = boost::thread(boost::bind(&ServiceImp::run, this));
}

void
ServiceImp::onStart()
{
	m_theUpdateCycle = boost::posix_time::seconds(5);
	m_theTimmer.expires_from_now(m_theUpdateCycle);
	m_theTimmer.async_wait(m_theStrand.wrap(boost::bind(&ServiceImp::onTimer, this)));
}

void 
ServiceImp::run()
{
	onStart();
	while(m_bContinueRunning)
	{
		m_theIoSvc.run_one();
	}
	onStop();
}

void 
ServiceImp::stop()
{
	m_bContinueRunning = false;
}

void
ServiceImp::onStop()
{

}

void
ServiceImp::wait()
{
	m_theThreads.join();
}

void
ServiceImp::postMsg(IMsg* pMsg)
{
	//
	//	post message by using io service
	//
	m_theIoSvc.post(boost::bind(&ServiceImp::onDispatchMsg, this, pMsg));
}

void
ServiceImp::onTimer()
{
	m_theTimmer.expires_from_now(m_theUpdateCycle);
	m_theTimmer.async_wait(m_theStrand.wrap(boost::bind(&ServiceImp::onTimer, this)));
}

void
ServiceImp::onDispatchMsg(IMsg* pMsg)
{
}

